var timeOutNews = null;

$(document).ready(function() {
   initActiveImage();
   initRotateNews();
});

function initActiveImage(){
    $(".main_image .desc").show(); //Show Banner
    $(".main_image .block").animate({ opacity: 0.85 }, 1 ); //Set Opacity
    $(".image_thumb ul li:first").addClass('active'); //Add the active class (highlights the very first list item by default)

    $(".image_thumb ul li").mouseover(function() {
        activeNews(this);
    });
}

function initRotateNews(){
    $(".slide-news").hover(function() {
        clearTimeout(timeOutNews);
    }, function() {
        timeOutNews = window.setInterval( function(){ rotateNews( $(".image_thumb ul"), "li" ); }, 5000 );
    });

    timeOutNews = window.setInterval( function(){ rotateNews( $(".image_thumb ul"), "li" ); }, 5000 );
}

function activeNews(li) {
        //Set Variables
        var mainImageHeight = $(".main_image ul li.active").find('.block').height(); //Find the height of the "block"

        if ($(li).is(".active")) {  //If the list item is active/selected, then...
            return false; // Don't click through - Prevents repetitive animations on active/selected list-item
        } else { //If not active then...
            //Animate the Description
            currentIndex = $(li).prevAll().length;
            $(".main_image .block").stop().animate({ opacity: 0, marginBottom: -mainImageHeight }, 250 , function() { //Pull the block down (negative bottom margin of its own height)
                $(".main_image ul li").removeClass('active'); //Remove class of 'active' on all list-items
                $(".main_image ul li:eq(" + currentIndex + "):not(.active)").addClass('active');  //Add class of 'active' on the selected list
                $(".main_image ul li:eq(" + currentIndex + ") .block").animate({ opacity: 0.85,  marginBottom: "0" }, 250 ); //swap the html of the block, then pull the block container back up and set opacity
            });
        }
        //Show active list-item
        $(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all list-items
        $(li).addClass('active');  //Add class of 'active' on the selected list
        return false;
}

function rotateNews( element, filter ) {
 if( $( filter+ ".active", element ).nextAll( filter + ":not(.active):eq(0)" ).length == 1 ) {
    activeNews($( filter+ ".active", element ).nextAll( filter + ":not(.active):eq(0)" ));
 } else if( $( filter+ ":eq(0):not(.active)", element ).length == 1 ) {
    activeNews($( filter+ ":eq(0):not(.active)", element ));
 }
}

function loadKindNews(_this){
    $.ajax({
        type: "GET",
        url: $(_this).attr('href'),
        data: {},
        success: function(data){
            $('#home-news').replaceWith(data);
            initActiveImage();
        }
    });
}